package com.heima.admin.interceptor;


import com.heima.model.admin.pojos.AdUser;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import thread.AdThreadLocalUtil;
import thread.WmThreadLocalUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AdminInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //网关已经过滤了不是登录的请求,所以这里拦截的都是已经登录的请求
        String userId = request.getHeader("userId");
        //将用户存入到当前线程
        if (!StringUtils.isEmpty(userId)){
            AdUser adUser = new AdUser();
            adUser.setId(Integer.valueOf(userId));
            AdThreadLocalUtil.setUser(adUser);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        AdThreadLocalUtil.clear();
    }
}
